package com.aceitunaproject.webclient.client;

import gwt.gwtisometricboard.client.BoardDrawer;
import gwt.gwtisometricboard.client.model.Tile;

import com.aceitunaproject.shared.gamelement.Installation;
import com.aceitunaproject.shared.gamelement.Settlement;
import com.aceitunaproject.shared.gamelement.Unit;
import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RootPanel;

import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.CaptionPanel;

public class Board {

	private OrdersPanel ordersPanel;
	private ElementDetails elementDetails = new ElementDetails();
	private DetailsPanel detailsPanel = new DetailsPanel(this);
	private VerticalPanel mainPanel = new VerticalPanel();
	private HorizontalPanel aPanel = new HorizontalPanel();
	private Button drawBoardButton = new Button("Draw Board");
	private Button zoomInButton = new Button("Zoom In");
	private Button zoomOutButton = new Button("Zoom Out");
	private RadioButton displayPositionOption = new RadioButton("ActionGroup",
			"Display Position");
	private RadioButton moveUnitOption = new RadioButton("ActionGroup",
			"Move Unit");
	private Label msgsLabel = new Label();
	protected static final int WIDTH = 1500, HEIGHT = 768;
	private Context2d context;
	private BoardDrawer bd;

	private BoardImageProvider imageProvider;
	private final VerticalPanel buttonsVerticalPanel = new VerticalPanel();
	private final CaptionPanel cptnpnlButtonPanel = new CaptionPanel("Actions");
	private final CaptionPanel cptnpnlBoardClickAction = new CaptionPanel(
			"Board Click Action:");
	private final VerticalPanel optionsVerticalPanel = new VerticalPanel();
	private final VerticalPanel optionsAndLabelVerticalPanel = new VerticalPanel();
	private final CaptionPanel cptnpnlActionResult = new CaptionPanel(
			"Action Result");
	private MainAceitunaClient aceitunaClient;
	private final Canvas canvas = Canvas.createIfSupported();

	public Board(Aceituna_webclient aceitunaClient) {
		ordersPanel = new OrdersPanel(aceitunaClient);
		this.aceitunaClient = aceitunaClient;

		if (canvas == null) {
			RootPanel
					.get("GameBoard")
					.add(new Label(
							"Sorry, your browser doesn't support the HTML5 Canvas element"));
			return;
		}

		setupCanvasAndContext();

		// initializes the image provider
		imageProvider = new BoardImageProvider();

		aPanel.add(cptnpnlButtonPanel);
		cptnpnlButtonPanel.setContentWidget(buttonsVerticalPanel);
		buttonsVerticalPanel.setSize("148px", "104px");
		detailsPanel.setSize("278px", "160px");
		buttonsVerticalPanel.add(drawBoardButton);
		drawBoardButton.setWidth("148px");

		// Listen for mouse events on the DrawBoard button.
		drawBoardButton.addClickHandler(new DrawBoardClickHandler());
		buttonsVerticalPanel.add(zoomInButton);
		zoomInButton.setWidth("148px");
		buttonsVerticalPanel.add(zoomOutButton);
		zoomOutButton.setWidth("148px");

		// Listen for mouse events on the Zoom Out button.
		zoomOutButton.addClickHandler(new ZoomOutButtonClickHandler());

		// Listen for mouse events on the Zoom In button.
		zoomInButton.addClickHandler(new ZoomInButonClickHandler());
		aPanel.add(optionsAndLabelVerticalPanel);
		optionsAndLabelVerticalPanel.add(cptnpnlBoardClickAction);
		cptnpnlBoardClickAction.setContentWidget(optionsVerticalPanel);
		optionsVerticalPanel.setSize("5cm", "46px");
		optionsVerticalPanel.add(displayPositionOption);

		// sets the default option.
		displayPositionOption.setValue(true);
		optionsVerticalPanel.add(moveUnitOption);
		optionsAndLabelVerticalPanel.add(cptnpnlActionResult);
		cptnpnlActionResult.setHeight("52px");
		cptnpnlActionResult.setContentWidget(msgsLabel);
		msgsLabel.setSize("5cm", "30px");

		aPanel.add(detailsPanel);
		aPanel.add(elementDetails);
		aPanel.add(ordersPanel);
		// adds the panel and the canvas to the main panel.
		mainPanel.add(aPanel);
		mainPanel.add(canvas);

		// // Associate the Main panel with the HTML host page.
		RootPanel.get("GameBoard").add(mainPanel);
		mainPanel.setSize("392px", "128px");

		//
		canvas.addClickHandler(new CanvasClickHandler());
		// initializes the board drawer.
		bd = new BoardDrawer(null, context, imageProvider, 12,12, 64, 128,
				true, true, true);
	}

	public void Display(Tile[] tiles) {

		bd.setTiles(tiles);

		// draws the board.
		bd.drawBoard();

	}

	/**
	 * Displays a message in the message label
	 * 
	 * @param message
	 *            the message to display
	 * 
	 */
	public void DisplayMessage(String message) {
		msgsLabel.setText(message);
	}

	private void setupCanvasAndContext() {
		canvas.setWidth(WIDTH + "px");
		canvas.setCoordinateSpaceWidth(WIDTH);
		canvas.setHeight(HEIGHT + "px");
		canvas.setCoordinateSpaceHeight(HEIGHT);

		context = canvas.getContext2d();
		context.setStrokeStyle("Black");
	}

	// Event Handler nested classes

	private final class CanvasClickHandler implements ClickHandler {

		public void onClick(ClickEvent event) {
			Tile t = bd
					.getTileFromScreenCoordinates(event.getX(), event.getY());

			String position = "Out of Board";

			if (t != null) {
				position = String.valueOf(t.getX()) + ","
						+ String.valueOf(t.getY());

				detailsPanel.setTile(((WebTile) t).getFullTile());
				if (aceitunaClient.getOrderInProgress() == EnumOrder.MoveUnit) {
					aceitunaClient.MoveUnit(elementDetails.getSelectedUnit(),
							((WebTile) t).getFullTile());
				}
			}

			if (displayPositionOption.getValue()) {

				msgsLabel.setText(" User Clicked on Tile : " + position);

			} else {

				if (t != null) {

					// moveUnitTo(t);

				}
			}

		}
	}

	private final class ZoomOutButtonClickHandler implements ClickHandler {
		public void onClick(ClickEvent event) {

			bd.setZoom(bd.getZoom() - 10);

			context.clearRect(0, 0, WIDTH, HEIGHT);
			bd.drawBoard();
			msgsLabel.setText("Zoom : " + String.valueOf(bd.getZoom()));

		}
	}

	private final class ZoomInButonClickHandler implements ClickHandler {
		public void onClick(ClickEvent event) {

			bd.setZoom(bd.getZoom() + 10);

			context.clearRect(0, 0, WIDTH, HEIGHT);
			bd.drawBoard();
			msgsLabel.setText("Zoom : " + String.valueOf(bd.getZoom()));

		}
	}

	private final class DrawBoardClickHandler implements ClickHandler {
		public void onClick(ClickEvent event) {

			bd.drawBoard();
			msgsLabel.setText("Board Drawn");
		}
	}

	public void unitSelected(Unit unit) {
		elementDetails.setUnit(unit);
		ordersPanel.setForUnit(unit);
	}

	public void installationSelected(Installation installation) {
		elementDetails.setInstallation(installation);
		ordersPanel.setForInstallation(installation);

	}

	public void settlementSelected(Settlement settlement) {
		elementDetails.setSettlement(settlement);
		ordersPanel.setForSettlement(settlement);
	}

	


	/**
	 * @return the ordersPanel
	 */
	public OrdersPanel getOrdersPanel() {
		return ordersPanel;
	}
	
	/**
	 * @return the ordersPanel
	 */
	public ElementDetails GetElementDetails() {
		return elementDetails;
	}
}
